package com.vivo.tasks.vivofx;


import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.retail.entity.ViDeaSnaction;
import com.vivo.retail.service.ViDeaSnactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskViSnaction {

    @Autowired
    private ViDeaSnactionService viDeaSnactionService;


    public void SyncData() {
        syncLastData("ah");
        syncLastData("js");
    }

    private void syncLastData(String area) {
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = new Date();

        ViDeaSnaction lastItem = new ViDeaSnaction();
        lastItem.setTableZone(area);
        lastItem = viDeaSnactionService.getLast(lastItem);

        if (lastItem == null) {
        } else {
            startDate = lastItem.getCreateDate();
            System.out.println("last change date is :" + startDate);
        }

        int pageStart = 1;
        int pageSize = 5000;

        Page<ViDeaSnaction> page = new Page<ViDeaSnaction>(pageStart, pageSize);
        ViDeaSnaction query = new ViDeaSnaction();
        query.setBeginCreatedate(startDate);
        query.setEndCreatedate(endDate);

        try {
            Helper.changeDB(area);
            Page<ViDeaSnaction> pageRes = viDeaSnactionService.findPage(page, query);
            DatabaseContextHolder.setMainDb();
            System.out.println("cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());

            saveSnList(page.getList(), area);

            for (int i = pageStart + 1; i <= pageRes.getTotalPage(); i++) {
                page.setPageNo(i);

                Helper.changeDB(area);
                pageRes = viDeaSnactionService.findPage(page, query);
                DatabaseContextHolder.setMainDb();
                System.out.println("cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());

                saveSnList(page.getList(), area);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get  sn action info from " + area + " end!");
    }


    private void saveSnList(List<ViDeaSnaction> list, String area) {
        List<ViDeaSnaction> snList = new ArrayList<ViDeaSnaction>();

        for (ViDeaSnaction snaction : list) {
            snaction.setIsTaskInsert(true);
            snaction.setId(IdGen.uuid());
            snaction.setTableZone(area);

            snList.add(snaction);

            if (snList.size() >= 1000) {
                saveBatch(snList, area);

                snList.clear();
            }
        }

        if (snList.size() > 0) {
            saveBatch(snList, area);
        }
    }


    private void saveBatch(List<ViDeaSnaction> snList, String area) {
        try {
            viDeaSnactionService.insertByBatch(snList, area);
        } catch (Exception e) {
            e.printStackTrace();
            for (ViDeaSnaction snaction : snList) {
                try {
                    viDeaSnactionService.save(snaction);
                }catch (Exception ex) {
                    System.out.println("Sn action数据已经存在！ err: " + ex.getMessage());
                }
            }
        }
    }
}
